<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic;


use app\common\model\TbWorktask;
use app\common\logic\BaseLogic;
use think\facade\Db;
use think\helper\Str;


/**
 * TbWorktask逻辑
 * Class TbWorktaskLogic
 * @package app\adminapi\logic
 */
class TbWorktaskLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/22 10:01
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            TbWorktask::create([
                '工单号' => $params['工单号'],
                '状态' => $params['状态'],
                '工单类型' => $params['工单类型'],
                '分配机构' => $params['分配机构'],
                '分配人员' => $params['分配人员'],
                '参与人员' => $params['参与人员'],
                '业务标识' => $params['业务标识'],
                '工单来源' => $params['工单来源'],
                '来源编号' => $params['来源编号'],
                '包裹编号' => $params['包裹编号'],
                '快递公司' => $params['快递公司'],
                '快递单号' => $params['快递单号'],
                '产品IMEI' => $params['产品IMEI'],
                '产品品牌' => $params['产品品牌'],
                '产品型号' => $params['产品型号'],
                '产品颜色' => $params['产品颜色'],
                '保单号' => $params['保单号'],
                '密码' => $params['密码'],
                '出厂日期' => $params['出厂日期'],
                '购买日期' => $params['购买日期'],
                '保修期限' => $params['保修期限'],
                '保修状态' => $params['保修状态'],
                '报修故障' => $params['报修故障'],
                '故障说明' => $params['故障说明'],
                '报修服务类型' => $params['报修服务类型'],
                '报修数量' => $params['报修数量'],
                '母单号' => $params['母单号'],
                '用户名称' => $params['用户名称'],
                '手机号码' => $params['手机号码'],
                '创建时间' => $params['创建时间'],
                '详细地址' => $params['详细地址'],
                '公司名称' => $params['公司名称'],
                '邮箱' => $params['邮箱'],
                '工单备注' => $params['工单备注'],
                '收货备注' => $params['收货备注'],
                '最终服务类型' => $params['最终服务类型'],
                '保修状态1' => $params['保修状态1'],
                '故障类型' => $params['故障类型'],
                '故障描述' => $params['故障描述'],
                '物料费用' => $params['物料费用'],
                '服务费' => $params['服务费'],
                '优惠费用' => $params['优惠费用'],
                '收费合计' => $params['收费合计'],
                '检修结果' => $params['检修结果'],
                '新SN' => $params['新SN'],
                '坏品入库库房' => $params['坏品入库库房'],
                '支付方式' => $params['支付方式'],
                '实收金额' => $params['实收金额'],
                '支付单号' => $params['支付单号'],
                '支付姓名' => $params['支付姓名'],
                '支付账户' => $params['支付账户'],
                '支付日期' => $params['支付日期'],
                '收款结果' => $params['收款结果'],
                '收款备注' => $params['收款备注'],
                '发货类型' => $params['发货类型'],
                '快递公司1' => $params['快递公司1'],
                '快递单号1' => $params['快递单号1'],
                '发货备注' => $params['发货备注'],
                '创建' => $params['创建'],
                '时间2' => $params['时间2'],
                '审核' => $params['审核'],
                '审核时间' => $params['审核时间'],
                '受理' => $params['受理'],
                '受理日期' => $params['受理日期'],
                '报价' => $params['报价'],
                '报价日期' => $params['报价日期'],
                '处理' => $params['处理'],
                '处理日期' => $params['处理日期'],
                '结单' => $params['结单'],
                '结单日期' => $params['结单日期'],
                '耗时' => $params['耗时'],
                '预计完成时间' => $params['预计完成时间'],
                '服务单状态' => $params['服务单状态'],
                '派单价格' => $params['派单价格'],
                '收货备注1' => $params['收货备注1'],
                '发货物流费用' => $params['发货物流费用'],
                '结算状态' => $params['结算状态'],
                '是否结算' => $params['是否结算'],
                '结算月份' => $params['结算月份'],
                '不结算原因' => $params['不结算原因'],
                '是否保内结算' => $params['是否保内结算'],
                '是否保外买断' => $params['是否保外买断'],
                '保内不结算原因' => $params['保内不结算原因'],
                '物流费结算金额' => $params['物流费结算金额'],
                '保内结算金额' => $params['保内结算金额'],
                '保外结算金额' => $params['保外结算金额'],
                '其他结算金额' => $params['其他结算金额'],
                '保外买断金额' => $params['保外买断金额'],
                '合计结算金额' => $params['合计结算金额'],
                '结算备注' => $params['结算备注'],
                '时间' => $params['时间']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/22 10:01
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            TbWorktask::where('id', $params['id'])->update([
                '工单号' => $params['工单号'],
                '状态' => $params['状态'],
                '工单类型' => $params['工单类型'],
                '分配机构' => $params['分配机构'],
                '分配人员' => $params['分配人员'],
                '参与人员' => $params['参与人员'],
                '业务标识' => $params['业务标识'],
                '工单来源' => $params['工单来源'],
                '来源编号' => $params['来源编号'],
                '包裹编号' => $params['包裹编号'],
                '快递公司' => $params['快递公司'],
                '快递单号' => $params['快递单号'],
                '产品IMEI' => $params['产品IMEI'],
                '产品品牌' => $params['产品品牌'],
                '产品型号' => $params['产品型号'],
                '产品颜色' => $params['产品颜色'],
                '保单号' => $params['保单号'],
                '密码' => $params['密码'],
                '出厂日期' => $params['出厂日期'],
                '购买日期' => $params['购买日期'],
                '保修期限' => $params['保修期限'],
                '保修状态' => $params['保修状态'],
                '报修故障' => $params['报修故障'],
                '故障说明' => $params['故障说明'],
                '报修服务类型' => $params['报修服务类型'],
                '报修数量' => $params['报修数量'],
                '母单号' => $params['母单号'],
                '用户名称' => $params['用户名称'],
                '手机号码' => $params['手机号码'],
                '创建时间' => $params['创建时间'],
                '详细地址' => $params['详细地址'],
                '公司名称' => $params['公司名称'],
                '邮箱' => $params['邮箱'],
                '工单备注' => $params['工单备注'],
                '收货备注' => $params['收货备注'],
                '最终服务类型' => $params['最终服务类型'],
                '保修状态1' => $params['保修状态1'],
                '故障类型' => $params['故障类型'],
                '故障描述' => $params['故障描述'],
                '物料费用' => $params['物料费用'],
                '服务费' => $params['服务费'],
                '优惠费用' => $params['优惠费用'],
                '收费合计' => $params['收费合计'],
                '检修结果' => $params['检修结果'],
                '新SN' => $params['新SN'],
                '坏品入库库房' => $params['坏品入库库房'],
                '支付方式' => $params['支付方式'],
                '实收金额' => $params['实收金额'],
                '支付单号' => $params['支付单号'],
                '支付姓名' => $params['支付姓名'],
                '支付账户' => $params['支付账户'],
                '支付日期' => $params['支付日期'],
                '收款结果' => $params['收款结果'],
                '收款备注' => $params['收款备注'],
                '发货类型' => $params['发货类型'],
                '快递公司1' => $params['快递公司1'],
                '快递单号1' => $params['快递单号1'],
                '发货备注' => $params['发货备注'],
                '创建' => $params['创建'],
                '时间2' => $params['时间2'],
                '审核' => $params['审核'],
                '审核时间' => $params['审核时间'],
                '受理' => $params['受理'],
                '受理日期' => $params['受理日期'],
                '报价' => $params['报价'],
                '报价日期' => $params['报价日期'],
                '处理' => $params['处理'],
                '处理日期' => $params['处理日期'],
                '结单' => $params['结单'],
                '结单日期' => $params['结单日期'],
                '耗时' => $params['耗时'],
                '预计完成时间' => $params['预计完成时间'],
                '服务单状态' => $params['服务单状态'],
                '派单价格' => $params['派单价格'],
                '收货备注1' => $params['收货备注1'],
                '发货物流费用' => $params['发货物流费用'],
                '结算状态' => $params['结算状态'],
                '是否结算' => $params['是否结算'],
                '结算月份' => $params['结算月份'],
                '不结算原因' => $params['不结算原因'],
                '是否保内结算' => $params['是否保内结算'],
                '是否保外买断' => $params['是否保外买断'],
                '保内不结算原因' => $params['保内不结算原因'],
                '物流费结算金额' => $params['物流费结算金额'],
                '保内结算金额' => $params['保内结算金额'],
                '保外结算金额' => $params['保外结算金额'],
                '其他结算金额' => $params['其他结算金额'],
                '保外买断金额' => $params['保外买断金额'],
                '合计结算金额' => $params['合计结算金额'],
                '结算备注' => $params['结算备注'],
                '时间' => $params['时间']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/22 10:01
     */
    public static function delete(array $params): bool
    {
        return TbWorktask::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/03/22 10:01
     */
    public static function detail($params): array
    {
        return TbWorktask::findOrEmpty($params['id'])->toArray();
    }


    public static function info($id)
    {
        $key ='detail:'.$id;
        if (cache($key)){
            return (cache($key));
        }
        $result = self::detail(['id'=>$id]);
        $result['bujian'] =  Str::random(8,1);
        $result['KBB'] =  Str::random(8,1);
        $result['kehujian'] =  Str::random(10);
        $result['KBB2'] =  strtoupper(Str::random(15));
        $result['KGB1'] =  strtoupper( Str::random(17));
        $result['KGB2'] =  substr($result['KGB1'],0,3).'****'.substr($result['KGB1'],7,10) ;
        cache($key,$result,86400*30);
        return $result;
    }



}